﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Master.Validator;
using RockCompany.Master.View;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI07A : ParentFrm
    {
        #region Properties
        private UserAuthenticate uAuth;
        private Translator trans;
        private DataTable prodImportDetail;
        private SI07AFormModel frmModel;
        private bool isPassed = false;
        private bool isNew = true;
        private string sUserName; 
        #endregion 

        #region Constructor
        public SI07A()
        {
            InitializeComponent();
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);
            trans = new Translator();
            trans.controlTranslate(this);
            initDefaultData();
        }

        public SI07A(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            InitializeComponent();
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);
            this.trans = translator;
            trans.controlTranslate(this);
            sUserName = (string.IsNullOrEmpty(sUName) ? "" : sUName);
        } 
        #endregion

        #region Utility Methods
        public void initExistData(DataTable selectProdImport)
        {
            this.prodImportDetail = selectProdImport;
            isNew = false;
        }

        public void initDefaultData()
        {
            frmModel = new SI07AFormModel();
            trans.controlTranslate(this);
            frmModel.translator = this.trans;

            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            PHIEU_NK_TP_CT_DTO temp1 = new PHIEU_NK_TP_CT_DTO();
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_CHON));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_SO_LO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_MA_MAU));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_DON_VI));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP));
            properties.Add(temp1.GetType().GetProperty(PHIEU_NK_TP_CT_DTO.N_GHI_CHU));

            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_SO_LO);
            readOnlyCols.Add(PHIEU_NK_TP_CT_DTO.N_MA_MAU);

            frmModel.buildDataGrid(dgvProdductList, properties, readOnlyCols);
            if (!isNew)
            {
                textBox_MSCT.Text = prodImportDetail.Rows[0].Field<string>(PHIEU_NK_TP_CT_DTO.N_MS_PHIEU_NK);
                textBox_MSCT.ReadOnly = !isNew;
                isPassed = Util.parseBoolean(prodImportDetail.Rows[0][PHIEU_NK_TP_CT_DTO.N_PHE_DUYET]);
                checkBox_Pass.Checked = isPassed;
                frmModel.prodImportDetail = this.prodImportDetail;
            }
            else
            {
                frmModel.prodImportDetail = frmModel.buildEmptyDataTable(properties);
            }
            dgvProdductList.DataSource = frmModel.prodImportDetail;
        }
        #endregion

        #region Event methods
        private void button_NewProdImportDetail_Click(object sender, EventArgs e)
        {
            SW01B matsWorksManage = new SW01B(trans, sUserName, this);
            matsWorksManage.callChooser(true);
            if (matsWorksManage.ShowDialog(this) == DialogResult.OK)
            {
                List<string> selectedWorkOrder = matsWorksManage.getSelectedWO();
                matsWorksManage.Dispose();
                if (frmModel.addNewMatOrderDetail(selectedWorkOrder))
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void dgvProdductList_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int num1_index = dgvProdductList.CurrentRow.Cells[PHIEU_NK_TP_CT_DTO.N_SL_THUC_NHAP].ColumnIndex;
            if (e.ColumnIndex == num1_index)
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_SaveProdImport_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox_MSCT.Text) && dgvProdductList.Rows.Count > 0 &&
    InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION)
                                                             , trans.getMessageCaption(CommonConstantIF.SI06_ADD_CONFIRM)))
            {
                PHIEU_NK_TP_CT_DTO curProdImport = new PHIEU_NK_TP_CT_DTO();
                curProdImport.MS_PHIEU_NK = textBox_MSCT.Text;
                curProdImport.PHE_DUYET = isPassed;
                curProdImport.CREATION_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);
                curProdImport.LAST_UPDATE_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);

                if (!isNew)
                {
                    if (frmModel.updateProdImport(curProdImport) && frmModel.saveData(curProdImport))
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                }
                else
                {
                    if (frmModel.saveData(curProdImport))
                    {
                        isNew = false;
                        textBox_MSCT.ReadOnly = true;
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
            else
                InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.REQUIRED_FIELD), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));

        }

        private void checkBox_Pass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = checkBox_Pass.Checked;
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
